package com.fingard.dsp.bank.directbank.qdcb01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;


public class QDCB01Base extends DirectBase{

    protected String bsnCode = "";
    protected String serialNo = "";
    protected String sign = "";//还不知道sign如何获取
    protected String nextPageSearchKey = "";////首次上送时为空，之后上送已报文返回的关键字为准

    protected void writeCommonHeader(XmlTextWriter xmlWriter) throws Exception {

        if (StringHelper.isNullOrEmpty(bsnCode)) {
            throw new Exception("bsnCode");
        }
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        xmlWriter.writeStartElement("QDBankData");
        xmlWriter.writeStartElement("opReq");
        xmlWriter.writeElementString("bsnCode",bsnCode);
        xmlWriter.writeElementString("cstNo",bankFront.loginName);
        xmlWriter.writeElementString("serialNo",serialNo);
        xmlWriter.writeElementString("reqTime", Format.DateTime14Format.format(new Date()));
    }
    //使用http协议进行传输
    protected String[] sendToBank(String p_reqStr) throws Exception {

        String[] retStr = new String[]{"", ""};
        String httpUrl = "";
        int index = p_reqStr.indexOf("<QDBankData>");
        int index1 = p_reqStr.indexOf("<nextPageSearchKey>");
        int index2 = p_reqStr.indexOf("</nextPageSearchKey");
        String p_head = p_reqStr.substring(0,index).replace("\r\n", "");

        String p_data = p_reqStr.substring(index).replace("\r\n","").replace(" ","");
        if(index1!=-1&&index2!=-1){
            p_data = clearWrapAndBlank(p_reqStr.substring(index,index1))+p_reqStr.substring(index1,index2).replace("\r\n","")+clearWrapAndBlank(p_reqStr.substring(index2));
        }
        String p_reqString = p_head+p_data;
        httpUrl = bankFront.serverURL;
        WriteBankLogLn("2.提交银行的地址：" + httpUrl);
        WriteBankLogStep2(p_reqString);
        //发送银行前置
        WebRequest tmpRequest = new WebRequest(httpUrl);
        tmpRequest.setConnectTimeout(10000);
        tmpRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        tmpRequest.setRequestProperty("connection", "Close");
        String charsetName = getCharset();
        byte[] tmpWriteBytes = p_reqString.getBytes(charsetName);
        retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
        WriteBankLogStep3(retStr[1]);

        return retStr;
    }
    //使用HTTP协议访问签名
    protected String[] sendToBankFrontSign(String p_reqStr) throws Exception {

        String[] retStr = new String[]{"", ""};
        String httpUrl = "";
        httpUrl = bankFront.signURL;
        WriteBankLogLn("2.提交签名的地址：" + httpUrl);
        WriteBankLogLn("3.签名原文"+p_reqStr);
        //发送银行前置
        WebRequest tmpRequest = new WebRequest(httpUrl);
        tmpRequest.setConnectTimeout(60000);
        tmpRequest.setRequestProperty("Content-Type", "INFOSEC_SIGN/1.0;charset=gb2312");
        tmpRequest.setRequestProperty("connection", "Close");
        String charsetName = "gb2312";
        byte[] tmpWriteBytes = p_reqStr.getBytes(charsetName);
        retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
        WriteBankLogLn("4.前置返回签名结果:"+retStr[1]);
        return retStr;
    }
    protected static boolean isActiveRetCode(String retCode) {
        if (retCode != null) {
            if (retCode.equals("0000")) {
                return true;
            }
        }
        return false;
    }

    protected String getCharset(){
        return getBankConfig().getCharset("GBK");
    }

    protected String clearWrapAndBlank(String data){
        return data.replace("\r\n","").replace(" ","");
    }
}
